/**
  ******************************************************************************
  * @file    DPC_STM32_Interface.c
  * @author  STMicroelectronics
  * @version 1.0.0
  * @date    01-Mar-2024
  * @brief   Source file for the library wrapper
  * @details This file wrappes all services to run the DPC Application
  ******************************************************************************
  *
  * Copyright (c) 2022(-2024) STMicroelectronics
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file in
  * the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  ******************************************************************************
  *
  */

/** @addtogroup STM32_DPOW
  * @brief  Digital Power application implemented with a STM32 MCU
  * @{
  */

/** @addtogroup STM32_INTERFACE
  * @{
  */

/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* Private includes ----------------------------------------------------------*/
#include "dpc_stm32_interface.h"
#include "biricha-dpow-lib.h"
#include "math.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* External variables --------------------------------------------------------*/
static const float fTimerIdxPrescalingRatio[] = { 32, 16, 8, 4, 2, 1, 1.0f/2, 1.0f/4 }; /* Table containing prescaling value for hrtimer clock*/

/* External functions ---------------------------------------------------------*/
/**
  * @brief Convert frequency to HRTIM ticks
  * @details These are using PRESCALER values from table
  * @details This calculates the number of HRTIM ticks from frequency
  * @param  hhrtim pointer to HAL HRTIM handle
  * @param  TimerIdx Timer index
  *                   This parameter can be one of the following values:
  *                   @arg HRTIM_TIMERINDEX_MASTER for master timer
  *                   @arg HRTIM_TIMERINDEX_TIMER_A for timer A
  *                   @arg HRTIM_TIMERINDEX_TIMER_B for timer B
  *                   @arg HRTIM_TIMERINDEX_TIMER_C for timer C
  *                   @arg HRTIM_TIMERINDEX_TIMER_D for timer D
  *                   @arg HRTIM_TIMERINDEX_TIMER_E for timer E
  *                   @arg HRTIM_TIMERINDEX_TIMER_F for timer F
  * @param  [in] Frequency in Hz to convert in Ticks
  * @retval HRTIM ticks value
  */
uint32_t HAL_HRTIM_FreqToTicks(HRTIM_HandleTypeDef *hhrtim, uint32_t TimerIdx, uint32_t Frequency)
{
  int iScaleIndex = __HAL_HRTIM_GETCLOCKPRESCALER(hhrtim, TimerIdx);
  uint32_t ticks = (uint32_t)(HAL_RCC_GetHCLKFreq() * fTimerIdxPrescalingRatio[iScaleIndex] / Frequency);

  return MIN(ticks, UINT32_MAX);
}

/**
  * @brief Convert nanoseconds to HRTIM ticks
  * @details These are based on a default value of FHRCK = HRTIM * 1ns
  * @details This calculates the number of HRTIM ticks from ns
  * @param  hhrtim pointer to HAL HRTIM handle
  * @param  TimerIdx Timer index
  *                   This parameter can be one of the following values:
  *                   @arg HRTIM_TIMERINDEX_MASTER for master timer
  *                   @arg HRTIM_TIMERINDEX_TIMER_A for timer A
  *                   @arg HRTIM_TIMERINDEX_TIMER_B for timer B
  *                   @arg HRTIM_TIMERINDEX_TIMER_C for timer C
  *                   @arg HRTIM_TIMERINDEX_TIMER_D for timer D
  *                   @arg HRTIM_TIMERINDEX_TIMER_E for timer E
  *                   @arg HRTIM_TIMERINDEX_TIMER_F for timer F
  * @param  [in] Ns to convert in Ticks
  * @retval HRTIM ticks value
  */
uint32_t HAL_HRTIM_NsToTicks(HRTIM_HandleTypeDef *hhrtim, uint32_t TimerIdx, uint32_t Ns)
{
  int iScaleIndex = __HAL_HRTIM_GETCLOCKPRESCALER(hhrtim, TimerIdx);
  uint32_t ticks = (uint32_t)(HAL_RCC_GetHCLKFreq() * fTimerIdxPrescalingRatio[iScaleIndex] * Ns * 1.0e-9f);

  return MIN(ticks, UINT32_MAX);
}

/**
  * @brief
  * @details
  * Depending on the CORDIC usage or not, it computes the ArcTan function
  *
  * @param     [in]  Id    Real part of the current
  * @param     [in]  Iq    Imaginary part of the current
  * @retval    [out] float Computed ArcTan value
  */
float __ArcTanFloat( float Id, float Iq )
{
    static float delta_phi;

    // Use Math function for computation
    delta_phi = atanf(Iq/Id);

    return delta_phi;
}

/**
  * @} end STM32_INTERFACE
  */

/**
  * @} end STM32_DPOW
  */
